/**
 * 
 */
package team.helpmephone.app.sms;

import java.util.ArrayList;
import java.util.List;

import team.helpmephone.app.R;
import team.helpmephone.app.common.ConstantHelpMePhone;
import team.helpmephone.app.common.DaoHelpMePhone;
import team.helpmephone.app.common.HelpMePhoneAction;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.telephony.SmsManager;
import android.util.Log;

public class ReceiverSms extends BroadcastReceiver {
	private static final String TAG = "SMS RECEIVER";

	private Context appContext;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.v(TAG, intent.getAction());
		if (appContext == null) {
			appContext = context.getApplicationContext();			
		}
		
		if (HelpMePhoneAction.ACTION_ACTIVE_CURRENT_STATE.equals(intent.getAction())) {
			sendMessage();
		} else {
			processSms();
		}
	}

	private void processSms() {
		Log.v(TAG, "processSms()");
		switch (getResultCode()) {
		case Activity.RESULT_OK:
			Log.v(TAG, "Activity.RESULT_OK");
			break;
		case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
			Log.v(TAG, "SmsManager.RESULT_ERROR_GENERIC_FAILURE");
			break;
		case SmsManager.RESULT_ERROR_NO_SERVICE:
			Log.v(TAG, "SmsManager.RESULT_ERROR_NO_SERVICE");
			break;
		case SmsManager.RESULT_ERROR_NULL_PDU:
			Log.v(TAG, "SmsManager.RESULT_ERROR_NULL_PDU");
			break;
		case SmsManager.RESULT_ERROR_RADIO_OFF:
			Log.v(TAG, "SmsManager.RESULT_ERROR_RADIO_OFF");
			break;
		}
	}

	private void sendMessage() {
		Log.v(TAG, "sendMessage()");
		DaoHelpMePhone dao = DaoHelpMePhone.getInstance();
		Resources resources = appContext.getResources();
		String number = dao.get(appContext, resources.getResourceEntryName(R.id.RECEIVER_TEXT));
		String message = dao.get(appContext, resources.getResourceEntryName(R.id.SEND_MESSAGE_TEXT));
		sendMessage(number, message);
	}

	private void sendMessage(String number, String message) {
		Log.v(TAG, "sendMessage(), number = " + number + ", messsage = " + message);
		List<String> numberList = new ArrayList<String>();
		if (number.indexOf("/") == ConstantHelpMePhone.DONT_CARE) {
			String[] temp = number.split(",");
			numberList.add(temp[1]);
		} else {
			String[] temps = number.split("/");
			for(String temp : temps) {
				String[] value = temp.split(",");
				numberList.add(value[1]);
			}
		}
		SmsManager manager = SmsManager.getDefault();
		PendingIntent sentPI = createPendingIntent(HelpMePhoneAction.ACTION_SENT_SMS);
		PendingIntent deliveredPI = createPendingIntent(HelpMePhoneAction.ACTION_DELIVERED_SMS);
		
		for (int i = 0, n = numberList.size(); i < n; i++) {
			manager.sendTextMessage(numberList.get(i), null, message, sentPI, deliveredPI);
		}
	}

	private PendingIntent createPendingIntent(String action) {
		Intent intent = new Intent(action);
		PendingIntent pi = PendingIntent.getBroadcast(appContext, 0, intent, 0);
		return pi;
	}
}
